// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package cn.dev8.sdk.kotlin

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.ui.Messages
import nbcp.base.comm.const
import nbcp.base.extend.HasValue
import nbcp.base.extend.Repeat
import nbcp.base.extend.Slice
import nbcp.base.extend.basicSame
import org.slf4j.LoggerFactory
import java.io.File

class JavaDocSwaggerAction : SwaggerAction() {
    companion object {
        private val logger = LoggerFactory.getLogger(javaClass.declaringClass);
    }


    override fun update(e: AnActionEvent) {
        val project = e.getData(PlatformDataKeys.PROJECT)
        e.presentation.isEnabled = project != null;
    }

    override fun actionPerformed(event: AnActionEvent) {
        val project = event.getData(PlatformDataKeys.PROJECT)

        if (project == null || !project.isOpen) return;

        var basePath = project.basePath!!;

        logger.warn(basePath)

        var count = walk(File(basePath));

        if (count == 0) {
            Messages.showMessageDialog(
                project,
                "该插件会根据 java doc 生成 Swagger 注解，在项目中没有发现要生成的Swagger注解!",
                "生成Swagger注解",
                Messages.getInformationIcon()
            )
        } else {
            Messages.showMessageDialog(
                project,
                "生成Swagger注解 完成. 处理了 ${count} 个文件, 请格式化后提交!",
                "生成Swagger注解",
                Messages.getInformationIcon()
            )
        }
    }

}
